#include <logbase.h>
#include <errno.h>
#include <stdarg.h>
#include <cstring.h>

int main()
{
    cstrDef(tmpStr);
    int a = 1, b = 2;
    cstrSprint(&tmpStr, 20, "my check print:a=%d,b=%d", a, b);
    logdebugc("%s, %d, %d.", tmpStr.str, tmpStr.total, tmpStr.size);
    cstrDef(tmpStr1);
    cstrCpCstr(&tmpStr1, &tmpStr);
    logdebugc("%s, %d, %d.", tmpStr1.str, tmpStr1.total, tmpStr1.size);
    cstrCatCstr(&tmpStr, &tmpStr1);
    logdebugc("%s, %d, %d.", tmpStr.str, tmpStr.total, tmpStr.size);
    cstrMvCstr(&tmpStr1, &tmpStr);
    logdebugc("%s, %d, %d.", tmpStr.str, tmpStr.total, tmpStr.size);
    logdebugc("%s, %d, %d.", tmpStr1.str, tmpStr1.total, tmpStr1.size);

    if (1)
    {
        // check 1
        cstrSprint(&tmpStr, 64, "my check print:a=%d,b=%d", a, b);
        logdebugc("%p,%s, %d, %d.", tmpStr.str, tmpStr.str, tmpStr.total, tmpStr.size);
        cstrResize(&tmpStr, 32);// memery not resize, old size biger than new size.
        logdebugc("%p,%s, %d, %d.", tmpStr.str, tmpStr.str, tmpStr.total, tmpStr.size);
        cstrResize(&tmpStr, 128 * 1024);// memery resize, old size littler than new size. maybe tmpStr ptr address will change!
        logdebugc("%p,%s, %d, %d.", tmpStr.str, tmpStr.str, tmpStr.total, tmpStr.size);
    }

    if (1)
    {
        // check 2
        logdebugc("check deep copy!");
        cstrSprint(&tmpStr, 64, "my check print:a=%d,b=%d", a, b);
        logdebugc("%p,%s, %d, %d.", tmpStr.str, tmpStr.str, tmpStr.total, tmpStr.size);
        cstrCpCstr(&tmpStr1, &tmpStr);// deep copy, diffrent space.
        logdebugc("str:%p,%s, %d, %d.", tmpStr.str, tmpStr.str, tmpStr.total, tmpStr.size);
        logdebugc("str1:%p,%s, %d, %d.", tmpStr1.str, tmpStr1.str, tmpStr1.total, tmpStr1.size);
        cstrFree(&tmpStr);// free tmpStr 
        cstrFree(&tmpStr1);
    }

    if (1)
    {
        // check 2
        logdebugc("check move string!");
        cstrSprint(&tmpStr, 64, "my check print:a=%d,b=%d", a, b);
        logdebugc("%p,%s, %d, %d.", tmpStr.str, tmpStr.str, tmpStr.total, tmpStr.size);
        cstrMvCstr(&tmpStr1, &tmpStr);// move string, tmpStr ptr move to tmpStr1, tmpStr is nullptr.
        logdebugc("str:%p,%s, %d, %d.", tmpStr.str, tmpStr.str, tmpStr.total, tmpStr.size);
        logdebugc("str1:%p,%s, %d, %d.", tmpStr1.str, tmpStr1.str, tmpStr1.total, tmpStr1.size);
        cstrFree(&tmpStr);// free tmpStr 
        cstrFree(&tmpStr1);
    }

    if (0)
    {
        // check 3
        logdebugc("check shallow copy!");
        cstrSprint(&tmpStr, 64, "my check print:a=%d,b=%d", a, b);
        logdebugc("%p,%s, %d, %d.", tmpStr.str, tmpStr.str, tmpStr.total, tmpStr.size);
        // this operate is ok in compile, but it is danger.
        tmpStr1 = tmpStr;// shallow copy. is same pointer address,need not use this way!
        logdebugc("str:%p,%s, %d, %d.", tmpStr.str, tmpStr.str, tmpStr.total, tmpStr.size);
        logdebugc("str1:%p,%s, %d, %d.", tmpStr1.str, tmpStr1.str, tmpStr1.total, tmpStr1.size);
        cstrFree(&tmpStr);
        cstrFree(&tmpStr1);// double free.
    }

    // every cstrDef value, must be free.
    cstrFree(&tmpStr);
    cstrFree(&tmpStr1);
    return 0;
}
